package com.mall.manager.schemamanage.controller;



import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.mall.manager.schemamanage.service.CSManageService;
import com.mall.untils.Page;
import com.mall.untils.StringUtil;

@Controller
public class CSManageController {
	Logger logger = LoggerFactory.getLogger(CSManageController.class);
	@Resource
	CSManageService cSManageService;

	/**
	 * @功能 	: 根据方案id查询的业务处理
	 * @param 	: paraMap
	 * @return 	:
	 */
	@RequestMapping(value = "/manager_message_add")
	public String manager_message_add(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
		Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
		int b_id = MapUtils.getIntValue(paraMap, "b_id");
		int id = MapUtils.getIntValue(paraMap, "id");
		if (id != 0) {
			Map<String, Object> message_map = cSManageService.find_message(paraMap);
			modelMap.addAttribute("message_map", message_map);
		}
		modelMap.addAttribute("b_id", b_id);
		modelMap.addAttribute("id", id);
		return "schemamanage/manager_message_add";
	}

	/**
	 * @功能 	: 对方案表的添加和修改业务处理
	 * @param 	: paraMap
	 * @return 	:
	 */
	@RequestMapping(value = "/manager_message_insert")
	public String contentform_schemamanage_add(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
		Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
		int b_id = MapUtils.getIntValue(paraMap, "b_id");
		int id = MapUtils.getIntValue(paraMap, "id");
		int addNum = cSManageService.contentform_schemamanage_add(paraMap);
		if (addNum > 0) {
			switch (b_id) {
			case 1:
				modelMap.addAttribute("info", "家长方案操作成功！");
				break;
			case 2:
				modelMap.addAttribute("info", "教师方案操作成功！");
				break;
			}
			//添加操作
			if(id==0){
			return "redirect:/manager_scheme_show?b_id=" + b_id;
			}else{
			//修改操作
			return "redirect:/manager_scheme_show?b_id=" + b_id;
			}
		}
		modelMap.addAttribute("info", "方案操作失败！");
		return "redirect:/manager_message_add";
	}

	
	/**
	 * @功能 	: 对方案表的分页和模糊查询的业务处理
	 * @param 	: paraMap
	 * @return 	:
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/manager_scheme_show")
	public String manager_scheme_show(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
		Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
		String title = MapUtils.getString(paraMap, "title");
		int b_id = MapUtils.getIntValue(paraMap, "b_id");
		int direction = MapUtils.getIntValue(paraMap, "direction");
		String age_stage = MapUtils.getString(paraMap, "age_stage");
		int week = MapUtils.getIntValue(paraMap, "week");
		Page page = cSManageService.manager_scheme_show(request);
		//默认查看家长方案
		if (b_id == 0){
 	    	  b_id = 1;
	    }
		modelMap.addAttribute("b_id", b_id);
		//用于点击搜索按钮是下拉框中的查询条件不变
		modelMap.addAttribute("direction", direction);
		modelMap.addAttribute("age_stage", age_stage);
		modelMap.addAttribute("week", week);
		//把查询出来的总条数和一页显示的页容量存在modelMap中
		HttpSession session = request.getSession();
		modelMap.addAttribute("total",session.getAttribute("total"));
		modelMap.addAttribute("pageSize",session.getAttribute("pageSize"));
		if (!StringUtils.isEmpty(title)) {
			title=StringUtil.getByteToString(title);
		}
		modelMap.addAttribute("title", title);
		if (null != page) {
			modelMap.addAttribute("paraMapList",(List<Map<String, Object>>) page.getResult());
			int totalpagecount = page.getTotalPageCount();
			modelMap.addAttribute("totalpagecount", totalpagecount);
			int currentpageno = page.getCurrentPageNo();
			modelMap.addAttribute("currentpageno", currentpageno);
			int previouspage = page.getCurrentPageNo() <= 1 ? 1 : page.getCurrentPageNo() - 1;
			modelMap.addAttribute("previouspage", previouspage);
			int nextpage = page.getCurrentPageNo() >= page.getTotalPageCount() ? page.getTotalPageCount() : page.getCurrentPageNo() + 1;
			modelMap.addAttribute("nextpage", nextpage);
			List<Integer> showNumList = page.getShowNumList();
			modelMap.addAttribute("pagenumlist", showNumList);
		}
		return "schemamanage/manager_scheme_show";
	}

	
	/**
	 * @功能 	: 对方案表的删除业务处理
	 * @param 	: paraMap
	 * @return 	:
	 */
	@RequestMapping(value = "/manager_message_delete")
	@ResponseBody
	public String contentform_schemamanage_delete(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
		Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
		int deleteNum = cSManageService.contentform_schemamanage_delete(paraMap);
		return String.valueOf(deleteNum);
	}

	
	/**
	 * @功能 	: 将预览页中所需要的数据存在session中
	 * @param 	: paraMap
	 * @return 	:
	 */
	@RequestMapping(value = "/manager_message_look_data")
	@ResponseBody
	public String manager_message_look_data(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
		Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
		String title = MapUtils.getString(paraMap, "title");
		String megagame_rule = MapUtils.getString(paraMap, "megagame_rule");
		String date = MapUtils.getString(paraMap, "date");
		String author = MapUtils.getString(paraMap, "author");
		HttpSession session = request.getSession();
		session.setAttribute("tit", title);
		session.setAttribute("megagame_rule", megagame_rule);
		session.setAttribute("date", date);
		session.setAttribute("author", author);
		return "1";
	}

	
	/**
	 * @功能 	: 把session中的数据取出存在modelMap中，可以在预览页面直接调用
	 * @param 	: paraMap
	 * @return 	:
	 */
	@RequestMapping(value = "/manager_message_look")
	public String manager_message_look(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
		HttpSession session = request.getSession();
		modelMap.addAttribute("tit", session.getAttribute("tit"));
		modelMap.addAttribute("megagame_rule",session.getAttribute("megagame_rule"));
		modelMap.addAttribute("date", session.getAttribute("date"));
		modelMap.addAttribute("author", session.getAttribute("author"));
		return "schemamanage/manager_message_look";
	}

	
	/**
	 * @功能 	: 点击图片时获取该方案的值ById
	 * @param 	: paraMap
	 * @return 	:
	 */
	@RequestMapping(value = "/manager_message_findid")
	public String manager_message_findid(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
		Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
		int id = MapUtils.getIntValue(paraMap, "id");
		if (id != 0) {
			Map<String, Object> message_map = cSManageService.find_message(paraMap);
			modelMap.addAttribute("message_map", message_map);
		}
		return "schemamanage/manager_message_look";
	}
}
